﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using ebay.Utility;
using System.Data;

namespace ebay.Dal
{
    public class Product
    {
        string insertCommandText = null;
        Dictionary<string, string> parasCol = new Dictionary<string, string>();
        public Product()
        {
            StringBuilder colBuilder = new StringBuilder();
            StringBuilder valBuilder = new StringBuilder();
            string cmdText = "Select t_ItemPropDesc.FSQLColumnName,t_ItemPropDesc.FDefaultValue from t_ItemPropDesc where t_ItemPropDesc.FItemClassID = 4";

            using (SqlDataReader proReader = SqlHelper.ExecuteReader(SqlHelper.K3DB_Conn, CommandType.Text, cmdText, null))
            {
                if (proReader.HasRows)
                {
                    while (proReader.Read())
                    {
                        string columnName = proReader.GetString(0);
                        string defaultValue = proReader.IsDBNull(1) ? null : proReader.GetString(1);
                        colBuilder.Append(columnName).Append(",");
                        valBuilder.Append("@").Append(columnName).Append(",");
                        parasCol.Add(columnName, defaultValue);
                    }
                    colBuilder.Append("FNumber,FShortNumber,FName,FItemID");
                    valBuilder.Append("@FNumber,@FShortNumber,@FName,@FItemID");
                    insertCommandText = "Insert Into t_ICItem (" + colBuilder.ToString() + ") Values ( " + valBuilder.ToString() + ")";
                }
                else
                {
                    throw new Exception("找不到K3数据库的产品表");
                }
            }
        }

        public void InsertK3(ebay.Model.Product productMode)
        {
            string cmdText = "Update Product Set FitemId = @FitemId Where ProductId = @ProductId";
            string t_item_cmdtext = "INSERT INTO t_Item (FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) VALUES(4,0,1,@FName,@FNumber,@FShortNumber,@FFullNumber,1,NEWID(),0);Select FItemID From t_Item Where FNumber = @FNumber And FItemClassID = 4;";
            SqlParameter[] itemParas = { 
                                       new SqlParameter("@FName",productMode.ProductName),
                                       new SqlParameter("@FNumber",productMode.ProductId),
                                       new SqlParameter("@FShortNumber",productMode.ProductId),
                                       new SqlParameter("@FFullNumber",productMode.ProductId)
                                       };
            SqlCommand command = new SqlCommand();
            command.CommandText = t_item_cmdtext;
            command.Parameters.AddRange(itemParas);
            SqlParameter[] productParas = new SqlParameter[parasCol.Count + 5];
            int index = 0;
            DateTime addDateTime = DateTime.Now;
            using (SqlConnection connection = new SqlConnection(SqlHelper.K3DB_Conn))
            {
                command.Connection = connection;
                connection.Open();
                using (SqlTransaction trans = connection.BeginTransaction(IsolationLevel.ReadUncommitted))
                {
                    command.Transaction = trans;
                    object fItemId = command.ExecuteScalar();
                    foreach (KeyValuePair<string, string> kv in parasCol)
                    {
                        switch (kv.Key)
                        {
                            case "FComCategoryID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.CategoryId);//类别
                                break;
                            case "FHelpCode":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.UserCode);//助记码
                                break;
                            case "FFullName":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.ProductName);//名称
                                break;
                            case "FLength":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PackageLength);//长
                                break;
                            case "FWidth":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PackageWidth);//宽
                                break;
                            case "FHeight":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PackageHeight);//高
                                break;
                            case "FSize":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PackageVolume);//体积
                                break;
                            case "FGrossWeight":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PackageWeight);//毛重
                                break;
                            case "FNetWeight":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.Special_PackageWeight);//净重
                                break;
                            case "F_104": 
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.IsOutOfStock);//是否缺货
                                break;
                            case "F_103":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.SalesLevel);//销售评级
                                break;
                            case "F_105":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.SalesStatus);//销售状态
                                break;
                            case "F_106":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.KeyWords);//关键字
                                break;
                            case "F_115":
                                productParas[index] = new SqlParameter("@" + kv.Key, DBNull.Value);//商品审核时间
                                break;
                            case "F_116":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PackageType);//包裹类型
                                break;
                            case "F_107":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.WarrantyDays);//质保期
                                break;
                            case "F_122":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.CustomsName);//报关名称
                                break;
                            case "FHSNumber":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.HSCode);//中国海关HS编码
                                break;
                            case "F_123":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.InternationalCode);//国际码
                                break;
                            //case "F_110":
                            //    productParas[index] = new SqlParameter("@" + kv.Key, productMode.ConditionId);//Condition
                            //    break;
                            case "F_108":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.DeliveryWarehouseId);//发货仓库
                                break;
                            case "F_109":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.DeliveryId);//发货方式
                                break;
                            case "F_110":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.IsNew);//是否新品
                                break;
                            case "F_111":
                                productParas[index] = new SqlParameter("@" + kv.Key, addDateTime);//商品添加时间
                                break;
                            case "F_120":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.Delivery);//交货期
                                break;
                            case "F_121":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.ExpectedSales);//预计销量
                                break;
                            case "F_112":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.MinProfit);//最低利润率
                                break;
                            case "F_128":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.SalesGroup);//销售组
                                break;
                            case "F_113":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.NewProductStatus);//新品状态
                                break;
                            case "FUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.BaseUnit);//基本计量单位
                                break;
                            case "FOrderUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.PurUnit);//采购计量单位
                                break;
                            case "FSaleUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.BaseUnit);//销售计量单位
                                break;
                            case "FProductUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.ProduceUnit);//生产计量单位
                                break;
                            case "FStoreUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.StoreUnit);//库存计量单位
                                break;
                            case "FBarCodeUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.LableUnit);//条码标签打印单位
                                break;
                            case "FComTypeID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.ProductType);//商品类型
                                break;
                            case "FUseState":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.UseStatusId);//使用状态
                                break;
                            case "FSellMethod":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.SalesMode);//售卖方式
                                break;
                            case "FTrack":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.ValuationMethod);//计价方法
                                break;
                            case "FAcctID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.StockSubjectId);//存货科目代码
                                break;
                            case "FSaleAcctID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.SalesSubjectId);//销售收入科目代码
                                break;
                            case "FCostAcctID":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.SalesCostSubjectId);//销售成本科目代码
                                break;
                            case "FComBrandID":
                                productParas[index] = new SqlParameter("@" + kv.Key, "");//品牌
                                break;
                            case "FKCCUnitID":
                                productParas[index] = new SqlParameter("@" + kv.Key, "");//品牌
                                break;
                            case "FUnitGroupID":
                                productParas[index] = new SqlParameter("@" + kv.Key, "");//基本计量单位组
                                break;
                            case "vwUnitGroup":
                                productParas[index] = new SqlParameter("@" + kv.Key, productMode.MeasureUnitsGroup);//计量单位组
                                break;
                            default:
                                if (kv.Value == null)
                                {
                                    productParas[index] = new SqlParameter("@" + kv.Key, DBNull.Value);
                                }
                                else
                                {
                                    productParas[index] = new SqlParameter("@" + kv.Key, kv.Value);
                                }
                                break;
                        }
                        index++;
                    }
                    index = parasCol.Count;
                    productParas[index] = new SqlParameter("@FNumber", productMode.ProductId);
                    index++;
                    productParas[index] = new SqlParameter("@FShortNumber", productMode.ProductId);
                    index++;
                    productParas[index] = new SqlParameter("@FName", productMode.ProductId);
                    index++;
                    productParas[index] = new SqlParameter("@FParentID", SqlDbType.Int);
                    productParas[index].Value = 0;
                    index++;
                    productParas[index] = new SqlParameter("@FItemID", fItemId);
                    //index++;
                    //productParas[index] = new SqlParameter("@FFullName", productMode.ProductName);
                    SqlHelper.ExecuteNonQuery(trans, CommandType.Text, insertCommandText, productParas);
                    try
                    {
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                    }

                    SqlParameter[] p ={new SqlParameter("@FitemId",SqlDbType.Int),
                                           new SqlParameter("@ProductId",SqlDbType.VarChar)};
                    p[0].Value = fItemId;
                    p[1].Value = productMode.ProductId;
                    try
                    {
                        SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, p);
                    }
                    catch
                    {

                    }
                }
            }
        }

    }
}

